import React from "react";
import GoodNode from "../../../Component/Good";
// import './index.css';
import TradeServer from "../../../Server/Trade/TradeServer";
import UserCheck from "../../../utils/UserCheck";
import URL from "../../../Server/URL";
import MessageUtil from "../../../utils/MessageUtil";
import Common from "../../../utils/Common";
import SortTabItem from "../../../Component/SortTabItem";
export default class extends React.Component{
    
    constructor(props){
        super();

        let goods=[];

        this.state={
            goods,
            allFlag:false,
            nearFlag:false,
            priceFlag:false,
            timeFlag:false,
            stateFlag:false,

            currentSelectType:0,//当前排序方式
        }

        this.Gen();
    }
    componentWillUnmount(){
        MessageUtil.ShowSuspention("");
    }
    componentWillMount(){
        MessageUtil.ShowSuspention("TradeDistribure");
    }
    Gen(){
        let goods=[];

        let order=[ "综合", "时间", "价格正序", "价格倒序", "时间倒序"];
        let state=[ "接单中","已接单","已完成","已取消"]

        TradeServer.SendSellSelectSendSellList(UserCheck.GetUserID(),this.state.currentSelectType,1)
        .then((response)=>{
            // console.log(response)
            response.data.forEach(i => {

                let stateInfo="";
                let color="";
                switch(i.sendSell.status){
                    case "0":
                        color="red";
                        // color="#5393EB";//blue
                        break;
                    case "1":
                        color="#61BC12";
                        break;
                    case "2":
                        color="#61BC12";//green
                        break;
                    default:
                        break;
                }
                stateInfo=<span>派单状态: <font color={color}>{state[i.sendSell.status]}</font></span>
                let timeStr=Common.GetTimeStr(i.sendSell.time);

                if(i.sendSell.status==="0"){
                    goods.push(
                        <a href={"/trade/distributeDetail/"+i.sendSell.id} alt="good">
                        <GoodNode header={ URL.GetURL(i.userHead)}
                        key={i}
                        insurance={false}
                        guaranteeMoney={false}
                        modify={false} order={true} 
                        heart={false} 
                        bag={false} delete={false} 
                        name={i.sendSell.title}
                        time={timeStr}
                        desc={`拍摄地点: ${i.sendSell.shootingAddress}`}
                        state={stateInfo}
                        price={i.sendSell.shootingMoney}
                        confirmBtn={"我要接单"}
                        />
                        </a>
                    );
                }
                else{
                    goods.push(
                        <a href={"/trade/distributeDetail/"+i.sendSell.id} alt="good">
                        <GoodNode header={ URL.GetURL(i.userHead)}
                        key={i}
                        insurance={false}
                        guaranteeMoney={false}
                        modify={false} order={true} 
                        heart={false} 
                        bag={false} delete={false} 
                        name={i.sendSell.title}
                        time={timeStr}
                        desc={`拍摄地点: ${i.sendSell.shootingAddress}`}
                        state={stateInfo}
                        price={i.sendSell.shootingMoney}
                        />
                        </a>
                    )
                }
                
            });

            this.setState({
                goods
            },()=>{
                MessageUtil.ChangeHeight();
            })
        });
    }
    ClickFlag(orderSelectType,orderType){
        let allFlag=true;
        let nearFlag=true;
        let priceFlag=true;
        let timeFlag=true;
        let stateFlag=true;
        // console.log(orderSelectType)
        // console.log(orderType)

        let currentSelectType=0;
        switch(orderSelectType){
            case 0:
                allFlag=false;
                currentSelectType=0;
                break;
            case 1:
                nearFlag=false;
                break;
            case 2:
                priceFlag=false;
                if(orderType===0){
                    currentSelectType=2;
                }
                if(orderType===1){
                    currentSelectType=3;
                }
                break;
            case 3:
                timeFlag=false;
                if(orderType===0){
                    currentSelectType=1;
                }
                if(orderType===1){
                    currentSelectType=4;
                }
                break;
            case 4:
                stateFlag=false;
                break;
            default:
                break;
        }
        // console.log(orderType)
        // console.log(currentSelectType);
        this.setState({
            allFlag,
            nearFlag,
            priceFlag,
            timeFlag,
            stateFlag,
            currentSelectType:currentSelectType,
        },()=>{
            this.Gen();
        });
    }
    
    render(){
        return(
            <div className="Courseware_Body">
                <div className="Home_Sort">
                    <span className="Home_Sort_Item">
                        <SortTabItem clear={this.state.allFlag} 
                        title="综合" Change={this.ClickFlag.bind(this)} orderSelectType={0}/>
                    </span>
                    <span className="Home_Sort_Item">
                        <SortTabItem clear={this.state.nearFlag} 
                        title="附近" Change={this.ClickFlag.bind(this)} orderSelectType={1}/>
                        </span>
                    <span className="Home_Sort_Item">
                        <SortTabItem clear={this.state.priceFlag} 
                        title="价格" Change={this.ClickFlag.bind(this)} orderSelectType={2}/>
                        </span>
                    <span className="Home_Sort_Item">
                        <SortTabItem clear={this.state.timeFlag} 
                        title="时间" Change={this.ClickFlag.bind(this)} orderSelectType={3}/>
                        </span>
                    <span className="Home_Sort_Item">
                        <SortTabItem clear={this.state.stateFlag} 
                        title="状态" Change={this.ClickFlag.bind(this)} orderSelectType={4}/>
                        </span>
                </div>
                <div className="">
                    {this.state.goods}
                </div>
            </div>
            
        )
    }
}